<!--
 * @description:
 * @author: cuicui
 * @Date: 2023-11-30 15:52:56
 * @LastEditTime: 2023-11-30 15:58:47
 * @LastEditors: cuicui
-->
<template>
  <a-modal
    title="新建任务"
    :width="640"
    :visible="visible"
    :confirmLoading="loading"
    @ok="() => { $emit('ok') }"
    @cancel="() => { $emit('cancel') }"
  >
    <a-spin :spinning="loading">
      <a-form :form="form" v-bind="formLayout">
        <a-form-item label="任务名称">
            <a-input  v-decorator="[
              'task-name',
              { rules: [{ required: true, message: '请输入任务名称!' }] },
            ]"></a-input>
        </a-form-item>
        <a-form-item label="任务说明">
            <a-textarea
                placeholder="任务说明"
                :auto-size="{ minRows: 3, maxRows: 5 }"
                v-decorator="[
                    'task-desc', { rules: [{ required: true, message: '请输入任务说明!' }] },
                ]"/>
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

  <script>
  import pick from 'lodash.pick'

  // 表单字段
  const fields = ['task-name', 'task-desc']

  export default {
    props: {
      visible: {
        type: Boolean,
        required: true
      },
      loading: {
        type: Boolean,
        default: () => false
      },
      model: {
        type: Object,
        default: () => null
      }
    },
    data () {
      this.formLayout = {
        labelCol: {
          xs: { span: 24 },
          sm: { span: 3 }
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 21 }
        }
      }
      return {
        form: this.$form.createForm(this)
      }
    },
    created () {
      // 防止表单未注册
      fields.forEach(v => this.form.getFieldDecorator(v))

      // 当 model 发生改变时，为表单设置值
      this.$watch('model', () => {
        this.model && this.form.setFieldsValue(pick(this.model, fields))
      })
    }
  }
  </script>
